﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OurMind.Controls.Primitives
{

    internal class NotifyCollectionChangedListener
    {
        private INotifyCollectionChanged _source;

        public NotifyCollectionChangedEventHandler CollectionChanged;

        public void SetSource(INotifyCollectionChanged newObj)
        {
            UnloadOld(_source);
            LoadNew(newObj);
        }

        private void LoadNew(INotifyCollectionChanged newObj)
        {
            _source = newObj;
            if (newObj == null) return;
            newObj.CollectionChanged += SourceCollectionChanged;
        }

        private void UnloadOld(INotifyCollectionChanged oldObj)
        {
            if (_source == null) return;
            oldObj.CollectionChanged -= SourceCollectionChanged;
        }

        private void SourceCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
        {
            CollectionChanged?.Invoke(sender, e);
        }

    
    }
}
